package algorithms.sort;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;

public abstract class SortingVisualization extends JFrame {
    protected int[] array;
    protected SortPanel sortPanel;

    /**
     * 初始化窗口大小并
     * @param array
     */
    public SortingVisualization(int[] array) {
        this.array = array;
        this.sortPanel = new SortPanel();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Sorting Visualization");
        setSize(900, 700);
        setLocationRelativeTo(null);

        add(sortPanel);
        setVisible(true);
    }

    public abstract void showSortingProcess();
//    public void bubbleSort() {
//        int n = array.length;
//        boolean swapped;
//
//        for (int i = 0; i < n - 1; i++) {
//            swapped = false;
//
//            // 进行一轮冒泡排序
//            for (int j = 0; j < n - i - 1; j++) {
//                if (array[j] > array[j + 1]) {
//                    int temp = array[j];
//                    array[j] = array[j + 1];
//                    array[j + 1] = temp;
//                    swapped = true;
//
//                    // 每次交换元素后进行图形重绘
//                    sortPanelRepaint();
//                }
//            }
//
//            // 如果没有发生交换，说明数组已经有序，提前结束排序
//            if (!swapped) {
//                break;
//            }
//        }
//    }

    /**
     * 重绘数组柱状图，刷新间隔10ms
     */
    protected void sortPanelRepaint() {
        sortPanel.repaint();
        showSleep();
    }

    private void showSleep() {
        try {
            Thread.sleep(20);  // 延迟以可视化排序过程
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class SortPanel extends JPanel {
        private static final int BAR_WIDTH =
                10;
        private static final int BAR_HEIGHT_FACTOR = 2;

        /**
         * 绘制柱状图
         * @param g the <code>Graphics</code> object to protect
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.BLACK);

            //根据数组的长度和里面最大值动态构建柱状图
            int asInt = Arrays.stream(array).max().getAsInt();
            int maxHight = 600/asInt;
            int len = 400/array.length;


            int x = 50; //初始化第一个柱与y轴的空隙
            for (int i = 0; i < array.length; i++) {
                int barHeight = array[i] * maxHight;
                int y = getHeight() - barHeight;
                g.fillRect(x, y, len, barHeight);
                x += len + len;
            }
        }
    }

//    public static void main(String[] args) {
//        int[] array = {50, 11, 70, 33, 88, 81, 17, 95, 73, 4, 31, 29, 49, 80, 23, 52, 42, 28, 16, 98, 34, 74, 51, 85, 10, 25, 71, 1, 47, 89, 75, 5, 45, 68, 65, 92, 77, 24, 46, 61, 94, 79, 6, 3, 35, 87, 18, 99, 21, 54, 59, 97, 55, 14, 36, 26, 19, 90, 63, 66, 15, 84, 82, 9, 43, 12, 7, 58, 22, 67, 86, 64, 13, 38, 39, 60, 57, 91, 30, 69, 41, 2, 8, 56, 37, 32, 93, 83, 76, 72, 48, 96, 62, 40, 78, 27, 20, 44, 53};  // 待排序的数组
//        SortingVisualization visualization = new SortingVisualization(array);
//        visualization.bubbleSort();  // 调用冒泡排序算法进行排序并可视化
//    }
}
